package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.dto.AdminMenuDto;
import cn.zcscloud.mall.dto.OmsOrderDto;
import cn.zcscloud.mall.dto.OmsOrderReturnApplyDto;
import cn.zcscloud.mall.interceptor.UserHolder;
import cn.zcscloud.mall.mbg.model.OmsCompanyAddress;
import cn.zcscloud.mall.mbg.model.OmsOrderItem;
import cn.zcscloud.mall.mbg.model.OmsOrderOperateHistory;
import cn.zcscloud.mall.mbg.model.OmsOrderReturnApply;
import cn.zcscloud.mall.service.LoginService;
import cn.zcscloud.mall.service.OmsCompanyAddressService;
import cn.zcscloud.mall.service.OmsOrderReturnApplyService;
import cn.zcscloud.mall.vo.OrderReturnApplyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/returnApply")
public class OmsOrderReturnApplyController {

    @Autowired
    private OmsOrderReturnApplyService orderReturnApplyService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private OmsCompanyAddressService companyAddressService;

    @GetMapping("/list")
    public R list(OrderReturnApplyVo vo) {
        LambdaQueryWrapper<OmsOrderReturnApply> qw = new LambdaQueryWrapper<>();
        qw.like(vo.getCreateTime() != null, OmsOrderReturnApply::getCreateTime, vo.getCreateTime())
                .eq(vo.getHandleMan() != null, OmsOrderReturnApply::getHandleMan, vo.getHandleMan())
                .like(vo.getHandleTime() != null, OmsOrderReturnApply::getHandleTime, vo.getHandleTime())
                .eq(vo.getId() != null, OmsOrderReturnApply::getId, vo.getId())
                .eq(vo.getStatus() != null, OmsOrderReturnApply::getStatus, vo.getStatus());

        //PageRequest pageRequest = new PageRequest();
        //pageRequest.setPageNum(vo.getPageNum());
        //pageRequest.setPageSize(vo.getPageSize());
        Page<OmsOrderReturnApply> applyPage = new Page<>(vo.getPageNum(), vo.getPageSize());
        Page<OmsOrderReturnApply> page = orderReturnApplyService.page(applyPage, qw);
        if (page == null) {
            return R.success(null);
        }
        PageRequest pageRequest = PageRequest.init(page);

        return R.success(pageRequest);
    }

    //获取退货申请详情
    @GetMapping("/{id}")
    public R getOrderInfo(@PathVariable("id") Long id) {
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OmsOrderReturnApply::getId, id);
        OmsOrderReturnApply apply = orderReturnApplyService.getOne(queryWrapper1);
        OmsOrderReturnApplyDto dto = new OmsOrderReturnApplyDto();
        BeanUtils.copyProperties(apply, dto);

        LambdaQueryWrapper<OmsCompanyAddress> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(OmsCompanyAddress::getId, apply.getCompanyAddressId());
        OmsCompanyAddress companyAddress = companyAddressService.getOne(queryWrapper2);

        List<OmsCompanyAddress> list = new ArrayList<>();
        list.add(companyAddress);
        dto.setCompanyAddress(list);

        return R.success(dto);
    }

    //修改退货申请状态
    @PostMapping("/update/status/{id}")
    public R updateStatus(@RequestBody OmsOrderReturnApply apply, @PathVariable("id") Long id) {
       /* LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderReturnApply::getId, id);
        OmsOrderReturnApply returnApply = orderReturnApplyService.getOne(queryWrapper);*/

        OmsOrderReturnApply returnApply = orderReturnApplyService.getById(id);
        returnApply.setHandleMan(loginService.findByUserId(UserHolder.getUserId()).getUsername());
        returnApply.setHandleNote("处理好了");
        returnApply.setHandleTime(new Date());
        returnApply.setCompanyAddressId(apply.getCompanyAddressId());
        returnApply.setStatus(apply.getStatus());
        returnApply.setReceiveMan(apply.getReceiveMan());
        returnApply.setReceiveNote(apply.getReceiveNote());
        returnApply.setReceiveTime(new Date());
        returnApply.setReturnAmount(apply.getReturnAmount());
        orderReturnApplyService.updateById(returnApply);
        return R.success("修改成功!");
    }

    //批量删除退货申请
    @PostMapping("/delete")
    public R delete(@RequestParam List<Long> ids) {
        List<OmsOrderReturnApply> list = orderReturnApplyService.listByIds(ids);
        for (OmsOrderReturnApply apply : list) {
            if (apply.getStatus() == 0 || apply.getStatus() == 1) {
                return R.error("当前申请未完成,不能删除!", null);
            }
        }
        orderReturnApplyService.removeByIds(ids);
        return R.success("删除成功!");
    }
}
